<?php
require 'config.php';
require CAMINHO_SERVIDOR . 'funcoes.php';
require 'login.class.php';
$login = new Login();

if ($login->verificar(URL . 'index.php')) {
    $query = "SELECT j.local AS `local`,DATE_FORMAT(j.data,'%d/%m/%Y') AS `data`, 
                    DATE_FORMAT(j.hora,'%H:%i') AS `hora`,cam.descricao AS campeonato,
                    ta.img AS img_a,tb.img AS img_b,ta.nome AS time_a,tb.nome AS time_b,j.`id_jogo`,
                    j.`id_time_A`,j.`id_time_B`,j.`descricao`,j.`placar_time_A`,j.`placar_time_B`,
                    j.`id_campeonato`,j.`status`,j.`local`,j.`obs_placar`,j.`diferenca_gols`,j.`id_rodada`,
                    DATE_FORMAT(j.`data_cadastro`,'%d/%m/%Y') AS `data_cadastro`
                    FROM tb_jogos j
                    INNER JOIN tb_time ta ON ta.id_time=j.id_time_A
                    INNER JOIN tb_time tb ON tb.id_time=j.id_time_B 
                    INNER JOIN tb_campeonato cam ON cam.id_campeonato=j.id_campeonato 
                    ORDER BY j.data_cadastro,j.data DESC";
    $jogos = $login->executaQueryAll($query);
    //funcoes::debuga($jogos);
}
?>
<html>
    <head>
        <title>Lista Jogos</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/classes.css">
        <link rel="stylesheet" type="text/css" href="css/carregador.css">
        <script language="JavaScript" src="js/carregador.js"></script>
        <style type="text/css">
            td{
                vertical-align: middle !important;
            }
            .largura300{
                width: 300px;
            }
            .largura200{
                width: 200px;
            }
            .largura100{
                width: 100px;
            }
            .largura70{
                width: 70px;
            }
            .largura50{
                width: 50px;
            }
        </style>
    </head>
    <body onload="__loadEsconde();" class="container">
        <div class="container-fluid" id="carregador_pai">
            <div class="container-fluid" id="carregador">
                <div class="textoAlinhamentoCentro">Aguarde o processo ...</div>
                <div id="carregador_fundo"><div id="barra_progresso"> </div></div>
            </div>
        </div>
        <div class="container-fluid margin10px">
            <a class="btn btn-info col-md-offset-0" href="<?php echo URL_ADMIN; ?>" >Início</a>
        </div>

        <div class="col-md-8 col-md-offset-2">
            <p style="background-color:#FFFF00; color:#DAA520; border-color: #FFD700;" class="alert text-center"><strong>Atenção! </strong>Jogos em cor amarela precisam ser processados para calcular a pontuação!</p>
        </div>

        <div class="container-fluid col-md-4 col-md-offset-4">
            <?php
            if (isset($_GET["msg"])) {
                if ($_GET["msg"] == 1) {
                    ?>
                    <div class="alert alert-success text-center">
                        Placar processado com sucesso!
                    </div>
                    <?php
                } else {
                    ?>
                    <div class="alert alert-danger text-center">
                        Um erro ocorreu ao tentar processar o placar!
                    </div>
                    <?php
                }
            }
            ?>
        </div>
        <div class="container-fluid">
            <div class="col-md-12 col-md-offset-0">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title text-center"><?php echo $jogos[0]["campeonato"]; ?></h3>
                    </div>
                    <div class="panel-body text-center">
                        <table class="table table-hover">
                            <thead>
                                <tr class="info">
                                    <th colspan="3" class="text-center">Jogo</th>
                                    <th class="text-center">Data</th>
                                    <th class="text-center">Horário</th>
                                    <th class="text-center">Ações</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                foreach ($jogos as $i => $jogo) {
                                    if ($jogo["status"] == "A") {
                                        $colorido = "style='background-color:#ffff66;'";
                                    } else {
                                        $colorido = "";
                                    }
                                    ?>
                                    <tr <?php echo $colorido; ?>>
                                        <td class="text-right"><?php echo$jogo['time_a']; ?></td>
                                        <td class="text-center" style="width: 15%">
                                            <img src="imagens/<?php echo $jogo["img_a"]; ?>" alt="<?php echo $jogo["time_a"]; ?>" height="30" width="30">
                                            <b style="color:#147dad;font-size:20px;text-align:center;"><?php echo $jogo['placar_time_A']; ?></b>
                                            x
                                            <b style="color:#147dad;font-size:20px;text-align:center;"><?php echo $jogo['placar_time_B']; ?></b>
                                            <img src="imagens/<?php echo $jogo["img_b"]; ?>" alt="<?php echo $jogo["time_b"]; ?>" height="30" width="30">
                                        </td>
                                        <td class=""><?php echo$jogo['time_b']; ?></td>
                                        <td class="text-center" style="width: 120px"><?php echo$jogo['data']; ?></td>
                                        <td class="text-center" style="width: 120px"><?php echo$jogo['hora']; ?></td>
                                        <td class="text-center" >
                                            <a class="btn btn-success" href="<?php echo URL ?>cadJogos.php?id_jogo=<?php echo$jogo['id_jogo']; ?>">Alterar Jogo</a> 
                                            <a class="btn btn-info" href="<?php echo URL ?>jogo.php?id_jogo=<?php echo$jogo['id_jogo']; ?>">Alterar Placar</a> 
                                            <a class="btn btn-danger" href="<?php echo URL ?>processaPontuacao.php?id_jogo=<?php echo$jogo['id_jogo']; ?>">Processar</a>
                                        </td>
                                    </tr>
                                    <?php
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>